package com.sharemarking.wa.platform.mapper;

import com.sharemarking.wa.common.entity.Order;
import com.sharemarking.wa.platform.dto.orderDispatch.DriverListDto;
import com.sharemarking.wa.platform.dto.orderDispatch.OrderListDto;
import com.sharemarking.wa.platform.dto.orderDispatch.WasteListDto;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Repository
public interface OrderDispatchMapper {
    List<OrderListDto> getList(Map map);

    Integer getListTotal(Map map);

    @Select("SELECT a.name AS wasteName,a.waste_Id,a.weight,b.pic FROM wa_order_details a LEFT JOIN wa_org_waste b ON a.waste_id = b.id " +
            "WHERE a.order_id = #{orderId}")
    List<WasteListDto> getWasteList(Integer orderId);

    @Select("SELECT a.member_id AS driverId,a.name,a.head_img AS pic," +
            "(SELECT 5 - count(*) FROM `wa_order` o\n" +
            "where\n" +
            "1=1\n" +
            "and o.driver_id = 50 -- 司机的member_id\n" +
            "and o.state in (7,8,9)\n" +
            "and o.arrival_time > CURRENT_DATE\n" +
            "and o.arrival_time < CURRENT_DATE + 1\n)" +
            " AS orderNumber" +
            " FROM wa_driver_verify a LEFT JOIN sys_org_member c ON c.org_id = a.org_id " +
            "WHERE a.exam_state = 1 AND c.member_id = #{memberId} AND (SELECT count(*) FROM `wa_order` o\n" +
            "where o.driver_id = a.member_id  and o.state in (7,8,9) and o.arrival_time > CURRENT_DATE and o.arrival_time < CURRENT_DATE + 1) < 5 LIMIT #{pageIndex},#{pageSize}")
    List<DriverListDto> getDriverList(Map map);

    @Select("SELECT COUNT(*) FROM wa_driver_verify a LEFT JOIN sys_org b ON a.org_id = b.id LEFT JOIN sys_org_member c ON c.org_id = b.id " +
            "WHERE a.exam_state = 1 AND c.member_id = #{memberId} AND (SELECT count(*) FROM `wa_order` o\n" +
            "where o.driver_id = a.member_id  and o.state in (7,8,9) and o.arrival_time > CURRENT_DATE and o.arrival_time < CURRENT_DATE + 1) <5 ")
    Integer getDriverListTotal(Map map);

    @Update("UPDATE wa_order SET transport_org_id = null, transport_org_member_id = null, driver_id = null,update_date = #{updateDate},state = #{state} WHERE code = #{code}")
    void cancelOrder(Order order);

    @Update("UPDATE wa_order SET transport_org_id = #{transportOrgId}, transport_org_member_id = #{transportOrgMemberId}, driver_id = #{driverId},update_date = #{updateDate},state = #{state},arrival_time=#{arrivalTime} WHERE code = #{code}")
    void assignDriver(Order order);

    @Update("UPDATE wa_order SET driver_id = #{driverId},update_date = #{updateDate},state = #{state} WHERE code = #{code}")
    void replaceDriver(Order order);

    @Select("SELECT count(*) FROM wa_order o\n" +
            "where\n" +
            "1=1\n" +
            "and o.driver_id = #{driverId}\n" +
            "and o.state in (7,8,9)" +
            "and o.arrival_time >= CURRENT_DATE\n" +
            "and o.arrival_time <= CURRENT_DATE + 1\n")
    Integer getDriverOrderNumber(Integer driverId);


}
